<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/2/21
 * Time: 13:25
 */
// Function to fetch the HTML content of a URL
//function fetch_html($url) {
//    // ... (same as before)
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // Limit the number of redirects
//    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set a timeout
//    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
//    $html = curl_exec($ch);
//    curl_close($ch);
//
//    return $html;
//}

// Function to extract links using regular expressions (not recommended for complex HTML)
function extract_links_with_regex($html, $base_url) {
    preg_match_all('/<a\s+[^>]*href=["\']([^"\']*)["\'][^>]*>/i', $html, $matches);
    $links = $matches[1];
    $resolved_links = [];
    foreach ($links as $link) {
        $resolved_links[] = filter_var($link, FILTER_VALIDATE_URL) ? $link : resolve_url($base_url, $link);
    }
    return array_unique($resolved_links);
}

// Function to resolve relative URLs to absolute URLs
function resolve_url($base, $relative) {
    $parsedBase = parse_url($base);
    $parsedRelative = parse_url($relative);

    if (isset($parsedRelative['scheme'])) {
        return $relative;
    }

    $url = $parsedBase['scheme'] . '://' . $parsedBase['host'];

    if (isset($parsedRelative['path'])) {
        $path = $parsedRelative['path'];
    } else {
        $path = $parsedBase['path'];
    }

    if (isset($parsedRelative['query'])) {
        $path .= '?' . $parsedRelative['query'];
    } else if (isset($parsedBase['query'])) {
        $path .= '?' . $parsedBase['query'];
    }

    return $url . rtrim(dirname($path), '/') . '/' . ltrim($path, '/');
}

// (resolve_url function remains the same)

// Function to recursively visit all links
function visit_links($urls, &$visited = []) {
    // ... (same as before, but using extract_links_with_regex)

    foreach ($urls as $url) {
        sleep(2);
        if (!in_array($url, $visited)) {
            $visited[] = $url;
            echo "Visiting: $url\n";
            $html = @file_get_contents($url);
            if ($html !== false) {
                $links = extract_links_with_regex($html, $url);
                visit_links($links, $visited);
            } else {
                echo "Failed to fetch $url\n";
            }
        }
    }
}
// 数据库配置信息
$servername = "121.40.110.232"; // 数据库服务器地址
$username = "so_wzscwl_com"; // 数据库用户名
$password = "za7rS5hfJhPL5LDD"; // 数据库密码
$dbname = "so_wzscwl_com"; // 数据库名

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询数据表 web 中的 id 字段
$sql = "SELECT id,web FROM web WHERE type = 2";
$result = $conn->query($sql);
$initial_urls = [];
if ($result->num_rows > 0) {
    // 输出每行数据中的 id 字段
    while($row = $result->fetch_assoc()) {
//        die('https://so.wzscwl.com/web?url='.$row['web'].'&id='.$row['id']);
        $initial_urls[] = 'https://so.wzscwl.com/web?url='.$row['web'].'&id='.$row['id'];
    }
} else {
    echo "0 结果";
}

// 关闭连接
$conn->close();
// Example usage


visit_links($initial_urls);
